package com.atguigu.edu.realtime.common.util;

import com.atguigu.edu.realtime.common.constant.Constant;
import com.ververica.cdc.connectors.mysql.source.MySqlSource;
import com.ververica.cdc.connectors.mysql.table.StartupOptions;
import com.ververica.cdc.debezium.JsonDebeziumDeserializationSchema;
import org.apache.flink.api.common.serialization.DeserializationSchema;
import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.kafka.clients.consumer.ConsumerConfig;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author WEIYUNHUI
 * @date 2024/12/20 11:37
 */
public class FlinkSourceUtil {

    public static MySqlSource<String> getMysqlSource(String database , String table ){
        MySqlSource<String> mySqlSource = MySqlSource.<String>builder()
                .hostname(Constant.MYSQL_HOST)
                .port(Constant.MYSQL_PORT)
                .databaseList( database) // 设置捕获的数据库， 如果需要同步整个数据库，请将 tableList 设置为 ".*".
                .tableList( database + "." + table ) // 设置捕获的表
                .username(Constant.MYSQL_USER_NAME)
                .password(Constant.MYSQL_PASSWORD)
                .deserializer(new JsonDebeziumDeserializationSchema()) // 将 SourceRecord 转换为 JSON 字符串
                .startupOptions(StartupOptions.initial())
                .build();

        return mySqlSource ;
    }

    public static KafkaSource<String> getKafkaSource( String topic , String groupId){
        KafkaSource<String> kafkaSource = KafkaSource.<String>builder()
                .setBootstrapServers(Constant.KAFKA_BROKERS)
                .setGroupId(groupId)
                .setTopics( topic )
                .setValueOnlyDeserializer(
                        //不支持null值的处理
                        //new SimpleStringSchema()

                        //自定义反序列化， 支持null值
                        new DeserializationSchema<String>() {
                            @Override
                            public String deserialize(byte[] message) throws IOException {

                                // 判断是否为空, 如果为空， 不进行反序列化
                                if(message != null ){
                                    return new String( message , StandardCharsets.UTF_8) ;
                                }

                                //如果为空，直接返回null
                                return null;
                            }

                            @Override
                            public boolean isEndOfStream(String nextElement) {
                                return false;   //无界流没有流的结束
                            }

                            @Override
                            public TypeInformation<String> getProducedType() {
                                //return Types.STRING ;
                                return BasicTypeInfo.STRING_TYPE_INFO;
                            }
                        }

                )
                //.setStartingOffsets(OffsetsInitializer.committedOffsets(OffsetResetStrategy.LATEST))
                .setStartingOffsets(OffsetsInitializer.earliest())
//                .setStartingOffsets(OffsetsInitializer.latest())
                .setProperty(ConsumerConfig.ISOLATION_LEVEL_CONFIG , "read_committed")
                .build();

        return kafkaSource ;
    }
}
